define(function() {
  var g_result;
  var android  = isAndroid();
  function init(ele,result,setting){
    renderPage();
    g_result = result;
    initAni(ele,0);
    setting = setting || {'gSet':{'transEff':'normal','shareInfo':{}}};
    if(setting.audio)
      mediaAudio(setting.audio);

    if(setting.gSet.transEff){
      require(['mobile/swpeff/'+ setting.gSet.transEff]);
    }else{
      require(['mobile/swpeff/normal']);
    }
    // if(isWeixin() && (setting.gSet.shareInfo.isgetUserInfo || setting.gSet.shareInfo.isshare)){
    //   require(['mobile/wechat'],function(wxshare){
    //     wxshare.init(setting.gSet.shareInfo);
    //   });
    // }
  }

  function renderPage(){
    var d, e, f = 1,atag = $("#Js-container").find('a'),
        g = window.screen.availWidth,
        h = window.screen.availHeight;
    if(g / h >= 320 / 486 ){
      f = h / 486;
      if(g == 320 && h == 486){
        $('.page-con').each(function(i){
          $(this).css({'top':parseInt($(this).css('top'))*f+"px",'left':parseInt($(this).css('left'))*f+"px"});
        });
      }
    }else{
      f = g / 320;
    }

    $("#mobileViewport").attr("content", "width=320, initial-scale=" + f + ", maximum-scale=" + f + ",user-scalable=no");
    atag.each(function(){
      if($(this).attr('inithref')){
        $(this).attr('href',$(this).attr('inithref'));
        $(this).removeAttr('inithref');
      }
    })
    // if(isWeixin() && isAndroid()){
    //   var jc_w = $("#Js-container").width();
    //   var jc_h = $("#Js-container").height();
    //   setTimeout(function(){
    //     $("#Js-container .li_div").css({marginTop:($(window).height() - 486)/2,marginLeft:($(window).width() - 320)/2});
    //   },10);
    //   var i = 320 / jc_w,
    //       j = 486 / jc_h,
    //       k = Math.max(i, j);
    //   k = k > 1 ? k : 160 * k, k = parseInt(k);
    //   var minscale = Math.min(320/486,$("#Js-container").width()/$("#Js-container").height());
    //   var sc = minscale*h/g;
    //   $("#mobileViewport").attr("content", "width=320,initial-scale=" + sc + ",maximum-scale=" + sc + ",user-scalable=no,target-densitydpi=" + k);
    // }
  }

  function isWeixin() {
    return "micromessenger" == navigator.userAgent.toLowerCase().match(/MicroMessenger/i) ? !0 : !1;
  }
  function isAndroid(){
    return navigator.userAgent.indexOf("Android") > -1 || navigator.userAgent.indexOf("Linux") > -1
  }

  function initAni(ele,index){
    //if(android){
    ele.find('.mobile-comppont').each(function(i){
      if(g_result[index].css[i].ani.length){
        $(this).attr('hasAni',true);//有无动画标示
        //$(this).css('opacity',0);
        $(this).show();
        playAni($(this),g_result[index].css[i].ani);
      }
    });
    //}
    //ele.find('.li_div').show();

  }

  function playAni(ele, aniSq){
    //ele.show();
    //if(android){
    var i = 0;
    if(aniSq.length > 1){
      if(i == (aniSq.length-1)){
        aniSq[i].anifimd = 'both';
      }else{
        aniSq[i].anifimd = 'forwards';
      }
      ele.css({'opacity':1,'animation': aniSq[i].aniname + ' ' + aniSq[i].durtime + 's ' + aniSq[i].anitimfn + ' ' + aniSq[i].delaytime + 's ' + aniSq[i].anifimd + ' ' + aniSq[i].lptimes});
      ele.on('webkitAnimationEnd',function(){
        i++;
        if (i >= aniSq.length) {
          ele.off("webkitAnimationEnd");
          return;
        }
        ele.css({'opacity':1,'animation':aniSq[i].aniname + ' ' + aniSq[i].durtime + 's ' + aniSq[i].anitimfn + ' ' + aniSq[i].delaytime + 's ' + aniSq[i].anifimd + ' ' + aniSq[i].lptimes});
      });
    }else{
      ele.css({'opacity':1,'animation':aniSq[0].aniname + ' ' + aniSq[0].durtime + 's ' + aniSq[0].anitimfn + ' ' + aniSq[0].delaytime + 's both ' + aniSq[0].lptimes});
    }
    //}

  }
  function clearAni(ele){
    //if(android){
    ele.find('.mobile-comppont').each(function(){
      if($(this).attr('hasAni')){
        $(this).css({'opacity':0,'animation':''});
        $(this).hide();
      }
    });
    //}
    //ele.find('.li_div').hide();
  }

  function mediaAudio(audioSrc){
    var media = document.createElement("audio");
    var audioPlayFlag = true;
    media.src = audioSrc;
    media.autoplay = true;
    media.loop = true;
    $('body').append(media);
    $('#Js-audio').on('click',function(){
      if(audioPlayFlag){
        $(this).removeClass('play_yinfu').addClass('off');
        $('#yinfu').removeClass("rotate");
        media.pause();
        audioPlayFlag = false;
      }else{
        $(this).removeClass('off').addClass('play_yinfu');
        $('#yinfu').addClass("rotate");
        media.play();
        audioPlayFlag = true;
      }
    });
  }
  window.initAni = initAni;
  window.clearAni = clearAni;
  return init;
})
